package org.android.sss;

import org.android.facebook.AsyncFacebookRunner;
import org.android.facebook.BaseRequestListener;
import org.android.facebook.Facebook;
import org.android.facebook.FacebookError;
import org.android.facebook.LoginButton;
import org.android.facebook.SessionEvents;
import org.android.facebook.SessionEvents.AuthListener;
import org.android.facebook.SessionEvents.LogoutListener;
import org.android.facebook.SessionStore;
import org.android.facebook.Util;
import org.android.tools.Global;
import org.android.tools.GpsLocation;
import org.android.tools.SSSHelper;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SSSAndroidActivity extends Activity {

	public static final String APP_ID = "121376047970842";


	private TextView mText;

	private Button buttonMenu;
	private LoginButton mLoginButton;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);



		buttonMenu = (Button)findViewById(R.id.buttonMenu);

		mLoginButton = (LoginButton) findViewById(R.id.login);
		mText = (TextView) SSSAndroidActivity.this.findViewById(R.id.txt);

		if (Global.fb_uid != Long.parseLong("0")) {
			mText.setText("Hello, " + Global.nameFB + "!");
			buttonMenu.setVisibility(Button.VISIBLE);
		}
		Global.mFacebook = new Facebook(APP_ID);
		Global.mAsyncRunner = new AsyncFacebookRunner(Global.mFacebook);

		SessionStore.restore(Global.mFacebook, this);
		SessionEvents.addAuthListener(new SampleAuthListener());
		SessionEvents.addLogoutListener(new SampleLogoutListener());
		mLoginButton.init(this, Global.mFacebook);

		buttonMenu.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				LocationManager mlocManager = (LocationManager)getSystemService(
						Context.LOCATION_SERVICE); 
				if(!mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER )) { 
					Toast.makeText( SSSAndroidActivity.this, "Please turn on GPS", Toast.LENGTH_SHORT ).show();
					Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS ); 
					startActivity(myIntent); 
				}
				else {
					Toast.makeText(SSSAndroidActivity.this, "GPS is already on", Toast.LENGTH_SHORT).show();
					Global.gps = new GpsLocation(SSSAndroidActivity.this);


					Global.xCoordinate = Global.gps.getGPS()[1];
					Global.yCoordinate = Global.gps.getGPS()[0];
					if (Global.xCoordinate < 15.0) {
						Double xTmp = Global.xCoordinate;
						Global.xCoordinate = Global.yCoordinate;
						Global.yCoordinate = xTmp;
					}
					Intent myIntent = new Intent(v.getContext(), MenuActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					finish();
					startActivityForResult(myIntent,0);
				}
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent data) {
		Global.mFacebook.authorizeCallback(requestCode, resultCode, data);
	}

	public class SampleAuthListener implements AuthListener {

		public void onAuthSucceed() {
			Global.mAsyncRunner.request("me", new SampleRequestListener());
		}

		public void onAuthFail(String error) {
			mText.setText(Global.fb_uid == Long.parseLong("0") ? "Login Failed: " + error : "Hello, " + Global.nameFB + "!");
		}
	}

	public class SampleLogoutListener implements LogoutListener {
		public void onLogoutBegin() {
			mText.setText("Logging out...");
		}

		public void onLogoutFinish() {
			mText.setText("You have logged out! ");
			buttonMenu.setVisibility(Button.INVISIBLE);
			Global.fb_uid = Long.parseLong("0");
		}
	}

	public class SampleRequestListener extends BaseRequestListener {

		public void onComplete(final String response, final Object state) {
			try {
				// process the response here: executed in background thread
				Log.d("Facebook-Example", "Response: " + response.toString());
				JSONObject json = Util.parseJson(response);
				final String name = json.getString("name");
				final String id = json.getString("id");

				SSSAndroidActivity.this.runOnUiThread(new Runnable() {
					public void run() {
						mText.setText("Hello, " + name + "!");
						Global.fb_uid = Long.parseLong(id);
						Global.nameFB = name;
						Global.permission = SSSHelper.checkUser(Global.fb_uid);
						if (Global.permission == 0) {
							Toast.makeText(SSSAndroidActivity.this, "Your account is blocked!", Toast.LENGTH_LONG).show();
						}
						if (Global.permission == 1) {
							Toast.makeText(SSSAndroidActivity.this, "You are logged in as normal user.", Toast.LENGTH_LONG).show();
							buttonMenu.setVisibility(Button.VISIBLE);
						}
						if (Global.permission == 2) {
							Toast.makeText(SSSAndroidActivity.this, "You are logged in as admin.", Toast.LENGTH_LONG).show();
							buttonMenu.setVisibility(Button.VISIBLE);
						}
						if (Global.permission == 3) {
							Toast.makeText(SSSAndroidActivity.this, "You are logged in as owner shop.", Toast.LENGTH_LONG).show();
							buttonMenu.setVisibility(Button.VISIBLE);
						}
						if (Global.permission == 4) {
							Toast.makeText(SSSAndroidActivity.this, "Hello first time in Support Shopping System! You are logged in as normal user!", Toast.LENGTH_LONG).show();
							buttonMenu.setVisibility(Button.VISIBLE);
						}

					}
				});
			} catch (JSONException e) {
				Log.w("Facebook-Example", "JSON Error in response");
			} catch (FacebookError e) {
				Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
			}
		}
	}
}